package com.starry.module.system.core.dict.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.starry.core.common.constants.CommonError;
import com.starry.core.common.domain.vo.PageVo;
import com.starry.core.common.execption.ServiceException;
import com.starry.db.core.enums.DBEnableDisableStatusEnum;
import com.starry.db.core.query.LambdaQueryWrapperX;
import com.starry.module.system.api.dict.domain.qo.SysDictionaryPageQo;
import com.starry.module.system.api.dict.domain.qo.SysDictionaryQo;
import com.starry.module.system.api.dict.domain.vo.SysDictionaryDtlVo;
import com.starry.module.system.api.dict.domain.vo.SysDictionaryVo;
import com.starry.module.system.core.dict.entity.SysDictionary;
import com.starry.module.system.core.dict.mapper.SysDictionaryMapper;
import com.starry.module.system.core.dict.service.SysDictionaryDataService;
import com.starry.module.system.core.dict.service.SysDictionaryService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 系统字典-服务实现
 *
 * @author xiaoke
 */
@Slf4j
@Service("sysDictionaryService")
public class SysDictionaryServiceImpl implements SysDictionaryService {

    private final SysDictionaryMapper sysDictionaryMapper;
    private final SysDictionaryDataService sysDictionaryDataService;

    public SysDictionaryServiceImpl(SysDictionaryMapper sysDictionaryMapper, SysDictionaryDataService sysDictionaryDataService) {
        this.sysDictionaryMapper = sysDictionaryMapper;
        this.sysDictionaryDataService = sysDictionaryDataService;
    }


    @PostConstruct
    @Override
    public void initSysDictionary() {
        log.info("初始化系统数据字典-start");
        List<SysDictionary> sysDictionaries = sysDictionaryMapper.selectList(null);
        for (SysDictionary sysDictionary : sysDictionaries) {
            sysDictionaryDataService.deleteDetailCache(sysDictionary.getCode());
            sysDictionaryDataService.getByDictType(sysDictionary.getCode());
        }
        log.info("初始化系统数据字典-end");
    }

    @Override
    public PageVo<SysDictionaryVo> page(SysDictionaryPageQo pageQo) {
        PageVo<SysDictionary> page = sysDictionaryMapper.selectPage(pageQo);
        List<SysDictionaryVo> vos = BeanUtil.copyToList(page.getRows(), SysDictionaryVo.class);
        return new PageVo<>(vos, page.getTotal());
    }

    @Override
    public SysDictionaryDtlVo getById(Long id) {
        SysDictionary sysDictionary = sysDictionaryMapper.selectById(id);
        if (sysDictionary == null) {
            throw new ServiceException(CommonError.DATA_NOT_EXISTS.getMsg());
        }
        return BeanUtil.toBean(sysDictionary, SysDictionaryDtlVo.class);
    }

    @Transactional
    @Override
    public Long save(SysDictionaryQo sysDictionaryQo) {
        LambdaQueryWrapperX<SysDictionary> wrapper = new LambdaQueryWrapperX<>();
        wrapper
                .eq(SysDictionary::getName, sysDictionaryQo.getName());
        if (sysDictionaryMapper.selectCount(wrapper) > 0) {
            throw new ServiceException(CommonError.DATA_EXISTS.getMsg());
        }
        SysDictionary sysDictionary = BeanUtil.toBean(sysDictionaryQo, SysDictionary.class);
        sysDictionary.setId(null);
        sysDictionaryMapper.insert(sysDictionary);
        return sysDictionary.getId();
    }

    @Transactional
    @Override
    public void updateById(SysDictionaryQo sysDictionaryQo) {
        if (sysDictionaryQo.getId() == null) {
            throw new ServiceException(CommonError.DATA_NOT_EXISTS.getMsg());
        }
        LambdaQueryWrapperX<SysDictionary> wrapper = new LambdaQueryWrapperX<>();
        wrapper
                .ne(SysDictionary::getId, sysDictionaryQo.getId())
                .and(w ->
                        w.eq(SysDictionary::getName, sysDictionaryQo.getName())
                );
        if (sysDictionaryMapper.selectCount(wrapper) > 0) {
            throw new ServiceException(CommonError.DATA_EXISTS.getMsg());
        }
        SysDictionary sysDictionary = sysDictionaryMapper.selectById(sysDictionaryQo.getId());
        if (sysDictionary == null) {
            throw new ServiceException(CommonError.DATA_NOT_EXISTS.getMsg());
        }
        BeanUtil.copyProperties(sysDictionaryQo, sysDictionary);
        sysDictionaryMapper.updateById(sysDictionary);

        // 删除字典内容缓存
        sysDictionaryDataService.deleteDetailCache(sysDictionary.getCode());
    }

    @Transactional
    @Override
    public void removeByIds(Set<Long> idList) {
        if (idList.isEmpty()) return;
        sysDictionaryMapper.deleteByIds(idList);
    }

    @Transactional
    @Override
    public void enable(Set<Long> idList) {
        if (idList.isEmpty()) return;
        sysDictionaryMapper.updateBatch(setDictionaryStatus(idList, DBEnableDisableStatusEnum.ENABLE));
    }

    @Transactional
    @Override
    public void disable(Set<Long> idList) {
        if (idList.isEmpty()) return;
        sysDictionaryMapper.updateBatch(setDictionaryStatus(idList, DBEnableDisableStatusEnum.DISABLE));
    }

    private Set<SysDictionary> setDictionaryStatus(Set<Long> idList, DBEnableDisableStatusEnum status) {
        Set<SysDictionary> sysDictionarys = new HashSet<>();
        idList.forEach(item -> {
            SysDictionary sysDictionary = new SysDictionary();
            sysDictionary.setId(item);
            sysDictionary.setStatus(status.getCode());
            sysDictionarys.add(sysDictionary);
        });
        return sysDictionarys;
    }


}

